<?php
namespace Index\Controller;
use Think\Controller;
use Index\Common\CommonController;
class SettingController extends CommonController {
    public function profile(){
        // $this->show('setting','utf-8');
        if ($_POST) {
            
            $birthday_y=I('post.birthday_y');
            $birthday_m=I('post.birthday_m');
            $birthday_d=I('post.birthday_d');
            // $birthday =$birthday_y .'-'.$birthday_m. '-' .$birthday_d;
            // var_dump($birthday);
            $province =I('post.selectp');
            $city =I('post.selectc');
            // $address =$selectp .'-'. $selectc;
            $jobs =I('post.job_id');

            // var_dump($introduce);exit;
            $introduce =I('post.signature');
            $web =I('post.homepage');
            $qq = I('post.qq');
            $phone = I('post.mobile');
            $mail = I('post.common_email');
            $sex = I('post.sex');
            $uid =$_SESSION['id'];
            if (!preg_match("/[0-9]{6,11}/", $qq)) {
                $this->error('QQ格式不正确，请重新输入');exit;
            }elseif (!preg_match("/[0-9]{11}/", $phone)) {
                $this->error('手机号码不正确，请重新输入');exit;
            }elseif (!preg_match("/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([.a-zA-Z0-9_-])+([.a-zA-Z0-9_-]+)+([.a-zA-Z0-9_-])$/", $mail)) {
                $this->error('邮箱格式不正确，请重新输入');exit;
            }else{
                // 链接数据库
            $link = M();
            $sql = "update user set birthday_y='{$birthday_y}',birthday_m='{$birthday_m}',birthday_d='{$birthday_d}',province='{$province}',city='{$city}',jobs='{$jobs}',introduce='{$introduce}',web='{$web}',qq='{$qq}',phone='{$phone}',mail='{$mail}',sex='{$sex}' where id='{$uid}'";
            // var_dump($sql);exit;
            $data =$link->execute($sql);
            // var_dump($data);
            $this->success('修改成功！');
            }
            
        }else{
            $uid =$_SESSION['id'];
            $link = M();
            // var_dump($link);
            $sql="select * from user where id='{$uid}'";
            // var_dump($sql);
            $data=$link->query($sql);

            $data = current($data);//把二维数组变成一维
            // var_dump($data);
            $this->assign('data', $data);//把$data中的数据分配到模板data

           $this->display('profile'); 
        }
        
    }

    public function head_upload(){
        if(!empty($_FILES)){
            // var_dump($_FILES);
            $config = array(
            'maxSize'    =>    3145728,// 设置附件上传大小
            'rootPath'   =>    './Uploads/face/',// 设置附件上传根目录
            'savePath'   =>    '',                  // 设置附件上传（子）目录
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),// 设置附件上传类型
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );


         $Upload  = new \Think\Upload($config);// 实例化上传类

         $data = $Upload->upload();//上传的文件
         // var_dump($data);exit;

         if (!$data) {
             $this->error($Upload->geterror());
         }else{
            $info =current($data);//获取上传的数组文件当前的元素
            // var_dump($info);
            $face = "/Uploads/face/".$info['savepath'].$info['savename'];//组上传文件路径
            // var_dump($face);exit;

            $uid =$_SESSION['id'];//获取当前上传的用户
            // var_dump($_SESSION);
            // 上传到数据库
            $link =M();

            $sql = "update user set head_img='{$face}' where id = '{$uid}' ";
            // var_dump(  $sql);

            $data = $link->execute($sql);
            // var_dump($data);
            // // 存头像SESSION
            $face = $_SESSION['face'];
            // 上传成功
            // $this->success('上传成功!',U('index/people/index'),2);
         }
     }else{
           $this->display('head_upload'); 
        }
    }

    public function security(){
        $uid=$_SESSION['id'];
        if ($_POST) {
            // var_dump($_POST);exit;
            $password=I('post.password');
            // var_dump($password);
            $newpassword1=I('post.newpassword1');
            $newpassword2=I('post.newpassword2');
            $link =M();
            // var_dump($link);
            $sql="select password from user where id='{$uid}'";
            // var_dump($sql);exit;
            $data=$link->query($sql);
            // var_dump($data);exit;
            if (md5($password)!=$data[0]['password']) {
                $this->error(请输入正确的密码);exit;
            }elseif (!preg_match("/[-_.a-zA-Z0-9]{6,12}/", $newpassword1)) { 
               $this->error(新密码格式不正确，请重新输入);exit;
            }elseif($newpassword1!=$newpassword2){
                $this->error(前后密码不一致，请重新输入);exit;
            }else{
                $password =md5($newpassword2);
                $sql ="update user set password='{$password}' where id ='{$uid}'";
                // var_dump($sql);
                $data =$link->execute($sql);
                // var_dump($data);
                $this->success(修改成功！,U('index/people/index'),2);
            }
            
        }else{
            $this->display('security');
        }

    	
    }


     public function verify(){
        $uid=$_SESSION['id'];
        if ($_POST) {
            $truename=I('post.name');
            $idcard=I('post.id_code');
            $phone=I('post.contact');
            $instruction=I('post.reason');
            // $preg="/[0-9]{18}/";
            if (empty($truename)) {
                $this->error(真实姓名不能为空，请重新输入);exit;
            }elseif (!preg_match("/[0-9]{18}/", $idcard)) {
                $this->error(身份证号码格式不正确，请重新输入);exit;
            }elseif (!preg_match("/[0-9]{11}/", $phone)) {
                $this->error(联系方式不格式不正确，请重新输入);exit;
            }
            elseif (empty($instruction)) {
                $this->error(认证说明不能为空，请重新输入);exit;
            }




            else{
                $link=M('verify');
                $sql="update verify set truename='{$truename}',idcard='{$idcard}',phone='{$phone}',instruction='{$instruction}' where uid='{$uid}'";
                // var_dump($sql);
                $data=$link->execute($sql);
                $this->success(提交成功！,U('index/people/index'),2);
            }




            
        }else{
            $this->display('verify');
        }
	
    }





}